package io.demo.qsprotobuf;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;

import com.google.common.collect.ImmutableList;

@SpringBootApplication
public class QsProtobufApplication {

    public static void main(String[] args) {
        SpringApplication.run(QsProtobufApplication.class, args);
    }

    @Bean
    ProtobufHttpMessageConverter protobufHttpMessageConverter() {
        return new ProtobufHttpMessageConverter();
    }

    @Bean
    public CourseRepository createTestCourses() {
        Map<Integer, Course> courses = new ConcurrentHashMap<>();
        Course course1 = Course.newBuilder()
            .setId(1)
            .setCourseName("REST with Spring")
            .addAllStudent(createTestStudents())
            .build();
        Course course2 = Course.newBuilder()
            .setId(2)
            .setCourseName("Learn Spring Security")
            .addAllStudent(Collections.emptyList())
            .build();
        courses.put(course1.getId(), course1);
        courses.put(course2.getId(), course2);
        return new CourseRepository(courses);
    }

    private Iterable<? extends Student> createTestStudents() {
        return new ImmutableList.Builder<Student>()
            .add(Student.newBuilder().setFirstName("John").setLastName("Doe").build())
            .add(Student.newBuilder().setFirstName("Jason").setLastName("Momoa").build())
            .add(Student.newBuilder().setFirstName("Harry").setLastName("Potter").build())
            .build();
    }
}
